package cn.cloudreal.cbms.mediaPrice.dao.impl;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.orm.ibatis.support.SqlMapClientDaoSupport;

import cn.cloudreal.cbms.common.util.PageBean;
import cn.cloudreal.cbms.mediaPrice.dao.MediaPriceDao;
import cn.cloudreal.cbms.vo.MediaPrice;
/**
 * <p>
 * Title: CBM
 * </p>
 * <p>
 * Description:媒体资源库Dao实现类
 * </p>
 * <p>
 * Copyright: Copyright (c) 2012
 * </p>
 * <p>
 * Company:云恒瑞通
 * </p>
 * 
 * @author liupanpan
 * @date 2012-07-09
 */
public class MediaPriceDaoimpl extends SqlMapClientDaoSupport implements MediaPriceDao
{

    /** 日志工具类实例 */
    private static final Logger log = Logger.getLogger(MediaPriceDaoimpl.class);
    private final String FIND_MediaPriceList = "mediaPrice.findMediaPriceList";
    private final String FIND_countMediaPrice = "mediaPrice.countMediaPrice";
    private final String ADD_MediaPrice = "mediaPrice.addMediaPrice";
    private final String DEL_MediaPrice = "mediaPrice.delMediaPrice";
    private final String FIND_MediaPriceBYPriceID = "mediaPrice.findAllMediaPriceByPriceID";
    private final String FIND_MediaPriceByPriceID_01 = "mediaPrice.findMediaPriceByPriceID";
    private final String FIND_MediaPriceByMediaIDOrName = "mediaPrice.findMediaPriceByMediaIDOrName";
    private final String FIND_MediaPriceByMediaIDOrName_01 = "mediaPrice.findMediaPriceByMediaIDOrName_01";
    private final String UPDATE_MediaPrice = "mediaPrice.updateMediaPrice";
    
    /**
     * 分页查询媒体列表
     */
    public List<MediaPrice> findMediaPriceList(PageBean page, MediaPrice mediaPrice)
    {
     // 打印进入方法体日志
        if (log.isInfoEnabled())
        {
            log.info("Enter MediaPriceDaoimpl findMediaPriceList() method");
        }
        
        int totalCount = (Integer) getSqlMapClientTemplate().queryForObject(FIND_countMediaPrice,mediaPrice);
        page.setTotalRecords(totalCount);
        if (totalCount == 0)
        {
            return null;
        }
        
        Map<String, Object> param = new HashMap<String, Object>();
        param.put("pageno", page.getPageNo());
        param.put("pagesize", page.getPageSize());
        param.put("contractYear", mediaPrice.getContractYear()); //年份
        param.put("promotionALastValidDate", mediaPrice.getPromotionALastValidDate()); //媒体名称
        param.put("mediaDiscountType", mediaPrice.getMediaDiscountType());//折扣类型
        param.put("city", mediaPrice.getCity());//地区
        param.put("mediaStartTime", mediaPrice.getMediaStartTime());//合同名称
        param.put("mediaEndTime", mediaPrice.getMediaEndTime());//媒体类型
        
        List<MediaPrice> list = this.getSqlMapClientTemplate().queryForList(FIND_MediaPriceList, param);
        
     // 打印退出方法体日志
        if (log.isInfoEnabled())
        {
            log.info("Exit MediaPriceDaoimpl findMediaPriceList() method");
        }
        return list;
    }

    public int addMediaPrice(MediaPrice mediaPrice)
    {
     // 打印进入方法体日志
        if (log.isInfoEnabled())
        {
            log.info("Enter MediaPriceDaoimpl addMediaPrice() method");
        }
        Integer id = (Integer) this.getSqlMapClientTemplate().insert(ADD_MediaPrice, mediaPrice);
     // 打印退出方法体日志
        if (log.isInfoEnabled())
        {
            log.info("Exit MediaPriceDaoimpl addMediaPrice() method");
        }
        return id;
    }

    /**
     * 删除媒体价格表
     */
    public void delMediaPrice(int priceID,int mediaID){
     // 打印进入方法体日志
        if (log.isInfoEnabled())
        {
            log.info("Enter MediaPriceDaoimpl delMediaPrice() method");
        }
        Map<String, Object> param = new HashMap<String, Object>();
        param.put("priceID", priceID);
        param.put("mediaID", mediaID);
        this.getSqlMapClientTemplate().delete(DEL_MediaPrice, param);
     // 打印进入方法体日志
        if (log.isInfoEnabled())
        {
            log.info("Exit MediaPriceDaoimpl delMediaPrice() method");
        }
    }

    public List<MediaPrice> findAllMediaPriceByPriceID(int priceID)
    {
        List<MediaPrice> list = this.getSqlMapClientTemplate().queryForList(FIND_MediaPriceBYPriceID,priceID);
        return list;
    }
    
    /**
     * 详情页面
     */
    public List<MediaPrice> findMediaPriceByPriceID(int priceID)
    {
        List<MediaPrice> list =  this.getSqlMapClientTemplate().queryForList(FIND_MediaPriceByPriceID_01,priceID);
        return list;
    }
    
    /**
     * 查找媒体价格
     */
    public List<MediaPrice> findMediaPriceByMediaIDOrName(Integer mediaID,String mediaName,String mediaType,String city){
        SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
        Date date = new Date();
        String nowDate = sf.format(date);
        Map<String, Object> param = new HashMap<String, Object>();
        param.put("mediaID", mediaID);//媒体编号
        param.put("mediaName", mediaName);//媒体名称
        param.put("payMode", mediaType);//媒体类型
        param.put("city", city);//地市
        param.put("nowDate", nowDate);
        List<MediaPrice> list = this.getSqlMapClientTemplate().queryForList(FIND_MediaPriceByMediaIDOrName,param);
        return list;
    }
    
    /**
     * 查找媒体名称查找
     */
    public List<MediaPrice> findMediaPriceByMediaIDOrName_01(String mediaName,String city,String nowDate){
        Map<String, Object> param = new HashMap<String, Object>();
        param.put("mediaName", mediaName);//媒体名称
        param.put("city", city);//地市
        param.put("nowDate", nowDate);//媒体名称
        List<MediaPrice> list = this.getSqlMapClientTemplate().queryForList(FIND_MediaPriceByMediaIDOrName_01,param);
        return list;
    }
    
    /**
     * 媒体价格修改
     * @param mediaPrice
     */
    public void updateMediaPrice(MediaPrice mediaPrice){
        this.getSqlMapClientTemplate().update(UPDATE_MediaPrice, mediaPrice);
    }
}
